
//
// 20120825 - AH - Created.
// 20120906 - AH - Missing java doc.
// 20120926 - AH - Expands now tree when get content.
// 20121111 - AH - addTreeSelectionListener(TreeSelectionListener) and
//                 removeTreeSelectionListener(TreeSelectionListener).
//

package org.japril.jhut.swing;

import java.awt.BorderLayout;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

import org.japril.jhut.framework.JhutTest;

/**
 * <p>
 *   Tree view for test hierarchy.
 * </p>
 * @author Arne Halvorsen (AH)
 */
@SuppressWarnings("serial")
public final class TestTreeView extends JPanel 
{
  private TestTreeModel m_model=TestTreeModel.EMPTY_MODEL;
  
  private final JTree m_tree=new JTree(m_model);
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public TestTreeView()
  {
    setLayout(new BorderLayout());
    add(new JScrollPane(m_tree), BorderLayout.CENTER);
    
    m_tree.setCellRenderer(new TestTreeCellRenderer());
    m_tree.setRootVisible(false);
  }
  
  /**
   * <p>
   *   Set content of view.
   * </p>
   * @param forest Content or {@code null} if to clear.
   */
  public void setContent(List<JhutTest> forest)
  {
    m_model=(forest==null) ? TestTreeModel.EMPTY_MODEL :
                             new TestTreeModel(forest);
    m_tree.setModel(m_model);
    
    expandAll(m_tree);
  }
  
  private void expandAll(JTree tree)
  {
    TreeModel model=tree.getModel();
    
    Object root=model.getRoot();

    // Traverse tree from root
    expandAll(tree, new TreePath(root), model);
  }
  
  private void expandAll(JTree tree, TreePath parent, TreeModel model)
  {
    // Traverse children
    Object node=parent.getLastPathComponent();
    
    int n=model.getChildCount(node);
    
    if (n>=0)
    {
      for (int i=0; i<n; i++)
      {
        Object child=model.getChild(node, i);
        TreePath path=parent.pathByAddingChild(child);
        expandAll(tree, path, model);
      }
    }

    tree.expandPath(parent);
  }
  
  /**
   * <p>
   *   Gets content of view.
   * </p>
   * @return Content.
   */
  public List<JhutTest> getContent(){ return m_model.getRoots(); }
  
  /**
   * <p>
   *   Gets selected test.
   * </p>
   * @return Selected or {@code null} if none selected.
   */
  public JhutTest getSelected()
  {
    TreePath tp=m_tree.getSelectionPath();
    if (tp!=null && tp.getPathCount()>0)
    {
      Object selected=tp.getLastPathComponent();
      if (selected instanceof JhutTest)
      {
        return (JhutTest)selected;
      }
    }
    
    return null;
  }
  
  /**
   * <p>
   *   Adds 
   *   {@link TreeSelectionListener}.
   * </p>
   * @param tsl Listener to add.
   */
  public void addTreeSelectionListener(TreeSelectionListener tsl)
  {
    if (tsl==null)
    {
      throw new NullPointerException("tsl");
    }
    
    m_tree.addTreeSelectionListener(tsl);
  }
  
  /**
   * <p>
   *   Removes 
   *   {@link TreeSelectionListener}.
   * </p>
   * @param tsl Listener to remove.
   */
  public void removeTreeSelectionListener(TreeSelectionListener tsl)
  {
    if (tsl==null)
    {
      throw new NullPointerException("tsl");
    }
    
    m_tree.removeTreeSelectionListener(tsl);
  }
  
}
